package com.ruoyi.utils;

import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.cfg.MapperConfig;
import com.fasterxml.jackson.databind.introspect.AnnotatedField;
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;

// 自定义命名策略
public class CustomSnakeCaseToCamelCaseStrategy extends PropertyNamingStrategy {
    @Override
    public String nameForField(MapperConfig<?> config, AnnotatedField field, String defaultName) {
        return camelToScreamingSnakeCase(defaultName);
    }

    @Override
    public String nameForGetterMethod(MapperConfig<?> config, AnnotatedMethod method, String defaultName) {
        return camelToScreamingSnakeCase(defaultName);
    }

    @Override
    public String nameForSetterMethod(MapperConfig<?> config, AnnotatedMethod method, String defaultName) {
        return camelToScreamingSnakeCase(defaultName);
    }

    /**
     * 将驼峰命名转换为大写蛇形命名法
     *
     * @param camelCaseStr 驼峰命名的字符串
     * @return 大写蛇形命名法的字符串
     */
    public  String camelToScreamingSnakeCase(String camelCaseStr) {
        if (camelCaseStr == null || camelCaseStr.isEmpty()) {
            return camelCaseStr;
        }
        if(camelCaseStr.equalsIgnoreCase("createDate")){
            return "CreateDate";
        }
        if(camelCaseStr.equalsIgnoreCase("lastModifiedDate")){
            return "LastModifiedDate";
        }
        if(camelCaseStr.equalsIgnoreCase("approveStatus")){
            return "ApproveStatus";
        }
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < camelCaseStr.length(); i++) {
            char ch = camelCaseStr.charAt(i);
            // 如果是大写字母，前面加上下划线并转换为大写
            if (Character.isUpperCase(ch)) {
                if (i > 0) {
                    result.append('_');
                }
                result.append(Character.toUpperCase(ch));
            } else {
                // 将小写字母直接转换为大写
                result.append(Character.toUpperCase(ch));
            }
        }
        return result.toString();
    }
}
